#ifndef FLASH_H
#define FLASH_H

#include "Arduino.h" // for byte data type
#include "SPI.h"

enum Result {BYTE_CACHED = 0, BYTE_BUFFERED = 1};
enum State {STATE_NONE, STATE_PROGRAM_LOAD, STATE_READ_FROM_CACHE, STATE_PROGRAM_EXECUTE, STATE_PAGE_READ};

class Flash
{	
  public:
    Result writeByte(uint8_t byte);
    Flash(uint32_t startAddress);
    
    	
    void writeEnable();
    
    void programLoadStart(int plane, uint16_t column);
    void programLoad(byte data);   
    void programLoadEnd();  

    void readFromCacheStart(int plane, uint16_t column);   
    int readFromCache();
    void readFromCacheEnd();

    void pageRead(uint32_t address);
    void programExecute(uint32_t address);  

    boolean changeState(State changeTo);

    uint16_t readID();
    
  private:		
    State state;
    
    char* buffer;
		
    uint32_t currentBlock;
  
    uint32_t currentByte;
    
    void reset();   
    int  operationInProgress();
    int  getFeature(int address);   
};

#endif
